package lex.mtd.core;

import java.util.ArrayList;

import javax.swing.event.ChangeListener;

import org.lex.utils.StateChangeSupport;
import org.lex.utils.SystemProperties;
import org.lex.utils.ToString;

import lex.mtd.core.utils.ProtocolUtils;

public class URLSet implements java.io.Serializable
{
	private static final long serialVersionUID = 3487273704412411112L;
	private ArrayList<String> urls = null;
	private transient StateChangeSupport scs = null;

	public URLSet()
	{
		this.urls = new ArrayList<String>();
		this.scs = new StateChangeSupport();
	}

	/**
	 * <code>l</code> will be notified if new url is added
	 * 
	 * @param l
	 */
	public void AddURLListener( ChangeListener l )
	{
		scs.addChangeListener( l );
	}

	public void removeURLListener( ChangeListener l )
	{
		scs.removeChangeListener( l );
	}

	public int getURLCount()
	{
		return urls.size();
	}

	public String getURL( int index )
	{
		return urls.get( index );
	}

	public void addURL( String ... urls )
	{
		if( null == urls || 0 == urls.length )
			return;
		for( String url : urls )
			if( ProtocolUtils.isSupportedURL( url ) && false == this.urls.contains( url ) )
			{
				this.urls.add( url );
				this.scs.fireStateChanged( url );
			}
	}

	@Override
	public String toString()
	{
		return ToString.toString( urls, "", "", SystemProperties.lineSeparator() );
	}

	public String toString( String sep )
	{
		return ToString.toString( urls, "", "", sep );
	}
}
